import java.util.ArrayList;
import java.util.List;

public class ExtractPackages {
    public static List<Packages> extractPackages(List<String> variables){
        List<Packages> packages = new ArrayList<Packages>();

        int len = 0;
        for(String str : variables){
            if(packages.isEmpty()){  // 对分离的第一个包进行保存
                Packages pack = new Packages();
                pack.classname = new ArrayList<String>();

                pack.packageName = extractJavaFileInfos(str).getPackagePath();
                pack.classname.add(extractJavaFileInfos(str).getFileName());
                pack.width = len;

                packages.add(pack);
            }
            else {
                String packName = extractJavaFileInfos(str).getPackagePath(); // 获取当前要处理类的包名、类名
                String className = extractJavaFileInfos(str).getFileName();

                if(packName.equals(packages.get(packages.size()-1).packageName)){
                    packages.get(packages.size()-1).width = ++len;
                    packages.get(packages.size()-1).classname.add(className);
                }
                else{
                    Packages pack = new Packages();
                    pack.classname = new ArrayList<String>();

                    pack.packageName = packName;
                    pack.classname.add(className);
                    pack.width = ++len;

                    packages.add(pack);
                }
            }
        }
        return packages;
    }
    private static JavaFileInfo extractJavaFileInfos(String filePath) {
        JavaFileInfo javaFileInfo = new JavaFileInfo();

        int lastSeparatorIndex = filePath.lastIndexOf("/");
        if (lastSeparatorIndex != -1) {
            String packagePath = filePath.substring(0, lastSeparatorIndex);
            String fileName = filePath.substring(lastSeparatorIndex + 1);
            javaFileInfo.setFileName(fileName);
            javaFileInfo.setPackagePath(packagePath);
        }

        return javaFileInfo;
    }
}
